<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图列表</title>
    <link rel="stylesheet" href="/plugins/element-ui/index.css">
    <style>
        .tableBar {
            display: flex;
            justify-content: space-between;
        }
        .tableBox {
            margin-top: 20px;
            margin-bottom: 20px;
            border-bottom: solid 2px #f3f4f7;
            border-top: solid 2px #f3f4f7;
        }
        .pageList {
            display: flex;
            justify-content: center;
        }
        .non {
            color: red;
        }
    </style>
</head>
<body>
<div class="dashboard-container" id="member-app">
    <div class="container">
        <div class="tableBar">
            <el-input
                    v-model="input"
                    placeholder="请输入轮播图详情"
                    style="width: 250px"
                    clearable
                    @keyup.enter.native="handleQuery">
                <i
                        slot="prefix"
                        class="el-input__icon el-icon-search"
                        style="cursor: pointer"
                        @click="handleQuery">
                </i>
            </el-input>
            <el-button
                    type="primary"
                    @click="addHandle('add')">
                + 添加轮播图
            </el-button>
        </div>
        <el-table
                :data="tableData"
                stripe
                size="mini"
                class="tableBox">
            <el-table-column
                    prop="headerimg"
                    label="轮播图"
                    align="center">
                <template slot-scope="scope">
                    <el-image style="width: auto; height: 50px; line-height: 50px; border: none; cursor: pointer;"
                              :src="getImage(scope.row.bannerImg)"
                              :preview-src-list="[ `/common/download?fileName=${scope.row.bannerImg}` ]" >
                        <div slot="error" class="image-slot">
                            <i class="el-icon-picture-outline"></i>
                        </div>
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column
                    prop="bannerDec"
                    label="详情"
                    align="center">
            </el-table-column>
            <el-table-column label="状态" width="100" align="center">
                <template slot-scope="scope">
                    <el-switch
                            v-model="scope.row.bannerStatus"
                            @change="statusHandle(scope.row)"
                            active-color="#13ce66"
                            inactive-color="#ff4949">
                    </el-switch>
                    <!--{{ String(scope.row.bannerStatus) === '0' ? '已禁用' : '正常' }}-->
                </template>
            </el-table-column>
            <el-table-column
                    label="操作"
                    align="center">
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            size="small"
                            class="blueBut"
                            @click="addHandle(scope.row.bannerId)">
                        编辑
                    </el-button>
                    <el-button
                            type="text"
                            size="small"
                            class="delBut non"
                            @click="removeHandle(scope.row.bannerId)">
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
                class="pageList"
                :page-size="pageSize"
                layout="total, prev, pager, next, jumper"
                :total="counts"
                :current-page.sync="page"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange">
        </el-pagination>
    </div>
</div>

<script src="/plugins/vue/vue.js"></script>
<script src="/plugins/element-ui/index.js"></script>
<script src="/plugins/axios/axios-0.18.0.js"></script>
<script src="/js/member.js"></script>
<script>
    new Vue({
        el: '#member-app',
        data() {
            return {
                input: '',
                counts: 0,
                page: 1,
                pageSize: 5,
                tableData : [],
                id: '',
                status: '',
            }
        },
        methods: {
            init() {
                const params = {
                    pageNum: this.page,
                    pageSize: this.pageSize,
                    bannerDec: this.input ? this.input : undefined,
                }
                axios.get('/banner/query', {params}).then(res => {
                    let info = res.data
                    if (info.code === 1) {
                        this.tableData = info.data.records || []
                        this.counts = info.data.total
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            // 查询
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 添加、修改
            addHandle (st) {
                if (st === 'add'){
                    window.location.href = '/page/banner/add.html'
                } else {
                    window.location.href = '/page/banner/add.html?id=' + st
                }
            },
            // 删除
            removeHandle(id) {
                this.$confirm("确认删除？", "提示", {type: "warning"}).then(() => {
                    axios.delete('/banner/' + id).then(res => {
                        let info = res.data
                        if (info.code === 1) {
                            this.init()
                            this.$message.success('轮播图删除成功！')
                        } else {
                            this.$message.error('轮播图删除失败！')
                        }
                    })
                })
            },
            // 状态修改
            statusHandle (row) {
                // this.id = row.bannerId
                // this.status = row.bannerStatus
                axios.put('/banner', row).then(res => {
                    let info = res.data
                    if (info.code === 1) {
                        this.$message.success('轮播图状态更改成功！')
                        this.handleQuery()
                    } else {
                        this.$message.error('轮播图状态更改失败！')
                    }
                })
            },
            getImage (image) {
                return `/common/download?fileName=${image}`
            },
            handleSizeChange (val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange (val) {
                this.page = val
                this.init()
            }
        },
        created() {
            this.init()
        },
    })
</script>
</body>
</html>
